/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package smartgrid;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;

import java.io.Serializable;
/**
 * Classe que representa um tarifário.
 */
public class Tariff implements Serializable {
	public Tariff() {
	}
	
	public static Tariff loadTariffByORMID(int ID) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return loadTariffByORMID(session, ID);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff getTariffByORMID(int ID) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return getTariffByORMID(session, ID);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff loadTariffByORMID(int ID, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return loadTariffByORMID(session, ID, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff getTariffByORMID(int ID, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return getTariffByORMID(session, ID, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff loadTariffByORMID(PersistentSession session, int ID) {
		try {
			return (Tariff) session.load(smartgrid.Tariff.class, new Integer(ID));
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff getTariffByORMID(PersistentSession session, int ID) {
		try {
			return (Tariff) session.get(smartgrid.Tariff.class, new Integer(ID));
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff loadTariffByORMID(PersistentSession session, int ID, org.hibernate.LockMode lockMode) {
		try {
			return (Tariff) session.load(smartgrid.Tariff.class, new Integer(ID), lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff getTariffByORMID(PersistentSession session, int ID, org.hibernate.LockMode lockMode) {
		try {
			return (Tariff) session.get(smartgrid.Tariff.class, new Integer(ID), lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff[] listTariffByQuery(String condition, String orderBy) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return listTariffByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff[] listTariffByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return listTariffByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff[] listTariffByQuery(PersistentSession session, String condition, String orderBy) {
		StringBuffer sb = new StringBuffer("From smartgrid.Tariff as Tariff");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Tariff[]) list.toArray(new Tariff[list.size()]);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff[] listTariffByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
		StringBuffer sb = new StringBuffer("From smartgrid.Tariff as Tariff");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Tariff[]) list.toArray(new Tariff[list.size()]);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff loadTariffByQuery(String condition, String orderBy) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return loadTariffByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff loadTariffByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return loadTariffByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff loadTariffByQuery(PersistentSession session, String condition, String orderBy) {
		Tariff[] tariffs = listTariffByQuery(session, condition, orderBy);
		if (tariffs != null && tariffs.length > 0)
			return tariffs[0];
		else
			return null;
	}
	
	public static Tariff loadTariffByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
		Tariff[] tariffs = listTariffByQuery(session, condition, orderBy, lockMode);
		if (tariffs != null && tariffs.length > 0)
			return tariffs[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateTariffByQuery(String condition, String orderBy) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return iterateTariffByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static java.util.Iterator iterateTariffByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
		try {
			PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
			return iterateTariffByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static java.util.Iterator iterateTariffByQuery(PersistentSession session, String condition, String orderBy) {
		StringBuffer sb = new StringBuffer("From smartgrid.Tariff as Tariff");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static java.util.Iterator iterateTariffByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
		StringBuffer sb = new StringBuffer("From smartgrid.Tariff as Tariff");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public static Tariff loadTariffByCriteria(TariffCriteria tariffCriteria) {
		Tariff[] tariffs = listTariffByCriteria(tariffCriteria);
		if(tariffs == null || tariffs.length == 0) {
			return null;
		}
		return tariffs[0];
	}
	
	public static Tariff[] listTariffByCriteria(TariffCriteria tariffCriteria) {
		return tariffCriteria.listTariff();
	}
	
	public static Tariff createTariff() {
		return new smartgrid.Tariff();
	}
	
	public boolean save() {
		try {
			smartgrid.SmartgridPersistentManager.instance().saveObject(this);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean delete() {
		try {
			smartgrid.SmartgridPersistentManager.instance().deleteObject(this);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean refresh() {
		try {
			smartgrid.SmartgridPersistentManager.instance().getSession().refresh(this);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean evict() {
		try {
			smartgrid.SmartgridPersistentManager.instance().getSession().evict(this);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean deleteAndDissociate() {
		try {
			if(getSupplier_Tariffs_role() != null) {
				getSupplier_Tariffs_role().tariff_Tariff_role.remove(this);
			}
			
			smartgrid.Contract[] lContract_PreviusTariffs_roles = contract_PreviusTariffs_role.toArray();
			for(int i = 0; i < lContract_PreviusTariffs_roles.length; i++) {
				lContract_PreviusTariffs_roles[i].tariff_PreviusTariffs_role.remove(this);
			}
			smartgrid.DataLine[] lDataLiness = dataLines.toArray();
			for(int i = 0; i < lDataLiness.length; i++) {
				lDataLiness[i].setTariff(null);
			}
			smartgrid.Price[] lPrice_Prices_roles = price_Prices_role.toArray();
			for(int i = 0; i < lPrice_Prices_roles.length; i++) {
				lPrice_Prices_roles[i].setTariff_Prices_role(null);
			}
			return delete();
		}
		catch(Exception e) {
			return false;
		}
	}
	
	public boolean deleteAndDissociate(org.orm.PersistentSession session) {
		try {
			if(getSupplier_Tariffs_role() != null) {
				getSupplier_Tariffs_role().tariff_Tariff_role.remove(this);
			}
			
			smartgrid.Contract[] lContract_PreviusTariffs_roles = contract_PreviusTariffs_role.toArray();
			for(int i = 0; i < lContract_PreviusTariffs_roles.length; i++) {
				lContract_PreviusTariffs_roles[i].tariff_PreviusTariffs_role.remove(this);
			}
			smartgrid.DataLine[] lDataLiness = dataLines.toArray();
			for(int i = 0; i < lDataLiness.length; i++) {
				lDataLiness[i].setTariff(null);
			}
			smartgrid.Price[] lPrice_Prices_roles = price_Prices_role.toArray();
			for(int i = 0; i < lPrice_Prices_roles.length; i++) {
				lPrice_Prices_roles[i].setTariff_Prices_role(null);
			}
			try {
				session.delete(this);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			return false;
		}
	}
	
	private java.util.Set this_getSet (int key) {
		if (key == smartgrid.ORMConstants.KEY_TARIFF_CONTRACT_PREVIUSTARIFFS_ROLE) {
			return ORM_contract_PreviusTariffs_role;
		}
		else if (key == smartgrid.ORMConstants.KEY_TARIFF_DATALINES) {
			return ORM_dataLines;
		}
		else if (key == smartgrid.ORMConstants.KEY_TARIFF_PRICE_PRICES_ROLE) {
			return ORM_price_Prices_role;
		}
		
		return null;
	}
	
	private void this_setOwner(Object owner, int key) {
		if (key == smartgrid.ORMConstants.KEY_TARIFF_SUPPLIER_TARIFFS_ROLE) {
			this.Supplier_Tariffs_role = (smartgrid.Supplier) owner;
		}
	}
	
	org.orm.util.ORMAdapter _ormAdapter = new org.orm.util.AbstractORMAdapter() {
		public java.util.Set getSet(int key) {
			return this_getSet(key);
		}
		
		public void setOwner(Object owner, int key) {
			this_setOwner(owner, key);
		}
		
	};
	
	private int ID;
	
	private smartgrid.Supplier Supplier_Tariffs_role;
	
	private String Name;
	
	private java.util.Set ORM_contract_PreviusTariffs_role = new java.util.HashSet();
	
	private java.util.Set ORM_dataLines = new java.util.HashSet();
	
	private java.util.Set ORM_price_Prices_role = new java.util.HashSet();
	
	private void setID(int value) {
		this.ID = value;
	}
	
	public int getID() {
		return ID;
	}
	
	public int getORMID() {
		return getID();
	}
	
	/**
	 * Nome do tarifário.
	 */
	public void setName(String value) {
		this.Name = value;
	}
	
	/**
	 * Nome do tarifário.
	 */
	public String getName() {
		return Name;
	}
	
	private void setORM_Contract_PreviusTariffs_role(java.util.Set value) {
		this.ORM_contract_PreviusTariffs_role = value;
	}
	
	private java.util.Set getORM_Contract_PreviusTariffs_role() {
		return ORM_contract_PreviusTariffs_role;
	}
	
	public final smartgrid.ContractSetCollection contract_PreviusTariffs_role = new smartgrid.ContractSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_TARIFF_CONTRACT_PREVIUSTARIFFS_ROLE, smartgrid.ORMConstants.KEY_CONTRACT_TARIFF_PREVIUSTARIFFS_ROLE, smartgrid.ORMConstants.KEY_MUL_MANY_TO_MANY);
	
	public void setSupplier_Tariffs_role(smartgrid.Supplier value) {
		if (Supplier_Tariffs_role != null) {
			Supplier_Tariffs_role.tariff_Tariff_role.remove(this);
		}
		if (value != null) {
			value.tariff_Tariff_role.add(this);
		}
	}
	
	public smartgrid.Supplier getSupplier_Tariffs_role() {
		return Supplier_Tariffs_role;
	}
	
	/**
	 * This method is for internal use only.
	 */
	private void setORM_Supplier_Tariffs_role(smartgrid.Supplier value) {
		this.Supplier_Tariffs_role = value;
	}
	
	private smartgrid.Supplier getORM_Supplier_Tariffs_role() {
		return Supplier_Tariffs_role;
	}
	
	private void setORM_DataLines(java.util.Set value) {
		this.ORM_dataLines = value;
	}
	
	private java.util.Set getORM_DataLines() {
		return ORM_dataLines;
	}
	
	public final smartgrid.DataLineSetCollection dataLines = new smartgrid.DataLineSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_TARIFF_DATALINES, smartgrid.ORMConstants.KEY_DATALINE_TARIFF, smartgrid.ORMConstants.KEY_MUL_ONE_TO_MANY);
	
	private void setORM_Price_Prices_role(java.util.Set value) {
		this.ORM_price_Prices_role = value;
	}
	
	private java.util.Set getORM_Price_Prices_role() {
		return ORM_price_Prices_role;
	}
	
	public final smartgrid.PriceSetCollection price_Prices_role = new smartgrid.PriceSetCollection(this, _ormAdapter, smartgrid.ORMConstants.KEY_TARIFF_PRICE_PRICES_ROLE, smartgrid.ORMConstants.KEY_PRICE_TARIFF_PRICES_ROLE, smartgrid.ORMConstants.KEY_MUL_ONE_TO_MANY);
	
	/**
	 * Retorna o objecto serializado no formato JSon.
	 */
	public String getJSon() {
		//TODO: Implement Method
		throw new UnsupportedOperationException();
	}
	
	public String toString() {
		return String.valueOf(getID());
	}
	
}
